﻿using System.Collections.Generic;
using System.Linq;
using GameOn.Web.Entities;

namespace GameOn.Web.Services
{
    public class MatchService : IMatchService
    {
        private GameOnEntities Entities { get; set; }

        public MatchService()
        {
            Entities = new GameOnEntities();
        }

        /// <summary>
        /// Returns a page of pool matches
        /// </summary>
        /// <param name="args">A dynamic object of Arguments. PageSize = The size of the page to return. If missing, will return all items</param>
        public List<Match> GetMatches(dynamic args)
        {
            if (args.PageSize != null)
            {
                return Entities.Matches
                    .Where(m=>m.PlayedDate != null && m.Winner != null)
                    .OrderByDescending(m=>m.Id)
                    .Take((int)args.PageSize)
                    .ToList();
            }

            return Entities.Matches.ToList();
        }

        /// <summary>
        /// Returns a page of pool matches for a player
        /// </summary>
        /// <param name="playerId">The player ID</param>
        /// <param name="args">A dynamic object of Arguments. PageSize = The size of the page to return. If missing, will return all items</param>
        public List<Match> GetMatches(int playerId, dynamic args)
        {
            var query = Entities.Matches.Where(m => m.PlayedDate != null && m.Winner != null && (m.Player1Id == playerId || m.Player2Id == playerId));

            if (args.PageSize != null)
            {
                query = query.OrderByDescending(m => m.Id).Take((int) args.PageSize);
            }

            return query.ToList();
        }
    }
}